package com.customer.controller.admin;

import com.alibaba.fastjson.JSON;
import com.aop.SysLog;
import com.customer.model.TblSysUser;
import com.customer.service.admin.AdminService;
import com.customer.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

// 管理模块
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/registerPage")
    public String registerPage() {
        return "/admin/register.html";
    }

    @RequestMapping("/loginPage")
    public String loginPage() {
        return "/admin/admin.html";
    }

    @RequestMapping("/repeat")
    public String repeat() {
        return "/admin/admin.html";
    }

    // 账号密码的验证
    @RequestMapping("/login")
    @ResponseBody
    @SysLog(module = "后端管理" , operation = "管理员登录"  , method = "admin_login")
    public String login(TblSysUser user, HttpServletRequest request) {
        // 进行管理员的账号密码验证
        System.out.println("user:" + user);
        ResponseData responseData = adminService.login(user);
        if (responseData.getCode() == 200) {
            // 登录成功，将登录用户保存到session中
            request.getSession().setAttribute("adminName", user.getUsername());
        }
        return JSON.toJSONString(responseData);
    }

    @ResponseBody
    @RequestMapping("/register")
    @SysLog(module = "后端管理" , operation = "管理员注册"  , method = "admin_register")
    public String register(TblSysUser user) {
        ResponseData responseData = adminService.register(user);
        return JSON.toJSONString(responseData);
    }
}
